
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>[物理内存探测与管理] · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part2.html" />
    
    
    <link rel="prev" href="introduction.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.6.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter8/introduction.html">
            
                <a href="../chapter8/introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter8/part1.html">
            
                <a href="../chapter8/part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter8/part1_1.html">
            
                <a href="../chapter8/part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter8/part2.html">
            
                <a href="../chapter8/part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter8/part3.html">
            
                <a href="../chapter8/part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter8/part4.html">
            
                <a href="../chapter8/part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter8/part5.html">
            
                <a href="../chapter8/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter9/introduction.html">
            
                <a href="../chapter9/introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter9/part1.html">
            
                <a href="../chapter9/part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter9/part2.html">
            
                <a href="../chapter9/part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter9/part3.html">
            
                <a href="../chapter9/part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter9/part4.html">
            
                <a href="../chapter9/part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter9/part5.html">
            
                <a href="../chapter9/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../exercise/introduction.html">
            
                <a href="../exercise/introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../exercise/part1.html">
            
                <a href="../exercise/part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../exercise/part2.html">
            
                <a href="../exercise/part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../exercise/part3.html">
            
                <a href="../exercise/part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../exercise/part4.html">
            
                <a href="../exercise/part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="../exercise/part5.html">
            
                <a href="../exercise/part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="../exercise/part6.html">
            
                <a href="../exercise/part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.7" data-path="../exercise/part7.html">
            
                <a href="../exercise/part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="../exercise/part8.html">
            
                <a href="../exercise/part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >[物理内存探测与管理]</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x7269;&#x7406;&#x5185;&#x5B58;&#x63A2;&#x6D4B;&#x4E0E;&#x7BA1;&#x7406;">&#x7269;&#x7406;&#x5185;&#x5B58;&#x63A2;&#x6D4B;&#x4E0E;&#x7BA1;&#x7406;</h2>
<ul>
<li><a href="https://github.com/rcore-os/rCore_tutorial/tree/ch4-pa1" target="_blank">&#x4EE3;&#x7801;</a></li>
</ul>
<p>&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;&#x7269;&#x7406;&#x5185;&#x5B58;&#x901A;&#x5E38;&#x662F;&#x4E00;&#x7247; RAM &#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x770B;&#x6210;&#x4E00;&#x4E2A;&#x4EE5;&#x5B57;&#x8282;&#x4E3A;&#x5355;&#x4F4D;&#x7684;&#x5927;&#x6570;&#x7EC4;&#xFF0C;&#x901A;&#x8FC7;&#x7269;&#x7406;&#x5730;&#x5740;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x4F4D;&#x7F6E;&#x8FDB;&#x884C;&#x8BFB;&#x5199;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x7269;&#x7406;&#x5730;&#x5740;<strong>&#x5E76;&#x4E0D;&#x4EC5;&#x4EC5;</strong>&#x53EA;&#x80FD;&#x8BBF;&#x95EE;&#x7269;&#x7406;&#x5185;&#x5B58;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#x5176;&#x4ED6;&#x7684;&#x5916;&#x8BBE;&#xFF0C;&#x56E0;&#x6B64;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E5F;&#x7B97;&#x662F;&#x4E00;&#x79CD;&#x5916;&#x8BBE;&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x8BBE;&#x8BA1;&#x662F;&#x56E0;&#x4E3A;&#xFF1A;&#x5982;&#x679C;&#x8BBF;&#x95EE;&#x5176;&#x4ED6;&#x5916;&#x8BBE;&#x8981;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x6307;&#x4EE4;&#xFF08;&#x5982; x86 &#x5355;&#x72EC;&#x63D0;&#x4F9B;&#x4E86;<strong>in</strong>, <strong>out</strong> &#x6307;&#x4EE4;&#x6765;&#x8BBF;&#x95EE;&#x4E0D;&#x540C;&#x4E8E;&#x5185;&#x5B58;&#x7684;<strong>IO</strong>&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF09;&#xFF0C;&#x4F1A;&#x6BD4;&#x8F83;&#x9EBB;&#x70E6;&#xFF0C;&#x4E8E;&#x662F;&#x5F88;&#x591A; CPU&#xFF08;&#x5982; RISC-V&#xFF0C;ARM&#xFF0C;MIPS &#x7B49;&#xFF09;&#x901A;&#x8FC7; MMIO(Memory Mapped I/O) &#x6280;&#x672F;&#x5C06;&#x5916;&#x8BBE;&#x6620;&#x5C04;&#x5230;&#x4E00;&#x6BB5;&#x7269;&#x7406;&#x5730;&#x5740;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x8BBF;&#x95EE;&#x5176;&#x4ED6;&#x5916;&#x8BBE;&#x5C31;&#x548C;&#x8BBF;&#x95EE;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E00;&#x6837;&#x5566;&#xFF01;</p>
<p>&#x6211;&#x4EEC;&#x5148;&#x4E0D;&#x7BA1;&#x90A3;&#x4E9B;&#x5916;&#x8BBE;&#xFF0C;&#x6765;&#x770B;&#x7269;&#x7406;&#x5185;&#x5B58;&#x3002;</p>
<h3 id="&#x7269;&#x7406;&#x5185;&#x5B58;&#x63A2;&#x6D4B;">&#x7269;&#x7406;&#x5185;&#x5B58;&#x63A2;&#x6D4B;</h3>
<p>&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x600E;&#x6837;&#x77E5;&#x9053;&#x7269;&#x7406;&#x5185;&#x5B58;&#x6240;&#x5728;&#x7684;&#x90A3;&#x6BB5;&#x7269;&#x7406;&#x5730;&#x5740;&#x5462;&#xFF1F;&#x5728; RISC-V &#x4E2D;&#xFF0C;&#x8FD9;&#x4E2A;&#x4E00;&#x822C;&#x662F;&#x7531; bootloader &#xFF0C;&#x5373; OpenSBI &#x6765;&#x5B8C;&#x6210;&#x7684;&#x3002;&#x5B83;&#x6765;&#x5B8C;&#x6210;&#x5BF9;&#x4E8E;&#x5305;&#x62EC;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5728;&#x5185;&#x7684;&#x5404;&#x5916;&#x8BBE;&#x7684;&#x626B;&#x63CF;&#xFF0C;&#x5C06;&#x626B;&#x63CF;&#x7ED3;&#x679C;&#x4EE5; DTB(Device Tree Blob) &#x7684;&#x683C;&#x5F0F;&#x4FDD;&#x5B58;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x5730;&#x65B9;&#x3002;&#x968F;&#x540E; OpenSBI &#x4F1A;&#x5C06;&#x5176;&#x5730;&#x5740;&#x4FDD;&#x5B58;&#x5728; <code>a1</code> &#x5BC4;&#x5B58;&#x5668;&#x4E2D;&#xFF0C;&#x7ED9;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x626B;&#x63CF;&#x7ED3;&#x679C;&#x63CF;&#x8FF0;&#x4E86;&#x6240;&#x6709;&#x5916;&#x8BBE;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5F53;&#x4E2D;&#x4E5F;&#x5305;&#x62EC; Qemu &#x6A21;&#x62DF;&#x7684; RISC-V &#x8BA1;&#x7B97;&#x673A;&#x4E2D;&#x7684;&#x7269;&#x7406;&#x5185;&#x5B58;&#x3002;</p>
<blockquote>
<p><strong>[info] Qemu &#x6A21;&#x62DF;&#x7684; RISC-V virt &#x8BA1;&#x7B97;&#x673A;&#x4E2D;&#x7684;&#x7269;&#x7406;&#x5185;&#x5B58;</strong></p>
<p>&#x901A;&#x8FC7;&#x67E5;&#x770B;<a href="https://github.com/qemu/qemu/blob/master/hw/riscv/virt.c" target="_blank">virt.c</a>&#x7684;<strong>virt_memmap[]</strong>&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x53EF;&#x4EE5;&#x4E86;&#x89E3;&#x5230; Qemu &#x6A21;&#x62DF;&#x7684; RISC-V virt &#x8BA1;&#x7B97;&#x673A;&#x7684;&#x8BE6;&#x7EC6;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5E03;&#x5C40;&#x3002;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x6574;&#x4E2A;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x6709;&#x4E0D;&#x5C11;&#x5185;&#x5B58;&#x7A7A;&#x6D1E;&#xFF08;&#x5373;&#x542B;&#x4E49;&#x4E3A;<strong>unmapped</strong>&#x7684;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF09;&#xFF0C;&#x4E5F;&#x6709;&#x5F88;&#x591A;&#x5916;&#x8BBE;&#x7279;&#x5B9A;&#x7684;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x770B;&#x4E0D;&#x61C2;&#x6CA1;&#x6709;&#x5173;&#x7CFB;&#xFF0C;&#x540E;&#x9762;&#x4F1A;&#x6162;&#x6162;&#x6D89;&#x53CA;&#x5230;&#x3002;&#x76EE;&#x524D;&#x53EA;&#x9700;&#x5173;&#x5FC3;&#x6700;&#x540E;&#x4E00;&#x5757;&#x542B;&#x4E49;&#x4E3A;<strong>DRAM</strong>&#x7684;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x8FD9;&#x5C31;&#x662F; OS &#x5C06;&#x8981;&#x7BA1;&#x7406;&#x7684; 128MB &#x7684;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x8D77;&#x59CB;&#x5730;&#x5740;</th>
<th style="text-align:left">&#x7EC8;&#x6B62;&#x5730;&#x5740;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">0x0</td>
<td style="text-align:left">0x100</td>
<td style="text-align:left">QEMU VIRT_DEBUG</td>
</tr>
<tr>
<td style="text-align:left">0x100</td>
<td style="text-align:left">0x1000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x1000</td>
<td style="text-align:left">0x12000</td>
<td style="text-align:left">QEMU MROM (&#x5305;&#x62EC; hard-coded reset vector; device tree)</td>
</tr>
<tr>
<td style="text-align:left">0x12000</td>
<td style="text-align:left">0x100000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x100000</td>
<td style="text-align:left">0x101000</td>
<td style="text-align:left">QEMU VIRT_TEST</td>
</tr>
<tr>
<td style="text-align:left">0x101000</td>
<td style="text-align:left">0x2000000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x2000000</td>
<td style="text-align:left">0x2010000</td>
<td style="text-align:left">QEMU VIRT_CLINT</td>
</tr>
<tr>
<td style="text-align:left">0x2010000</td>
<td style="text-align:left">0x3000000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x3000000</td>
<td style="text-align:left">0x3010000</td>
<td style="text-align:left">QEMU VIRT_PCIE_PIO</td>
</tr>
<tr>
<td style="text-align:left">0x3010000</td>
<td style="text-align:left">0xc000000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0xc000000</td>
<td style="text-align:left">0x10000000</td>
<td style="text-align:left">QEMU VIRT_PLIC</td>
</tr>
<tr>
<td style="text-align:left">0x10000000</td>
<td style="text-align:left">0x10000100</td>
<td style="text-align:left">QEMU VIRT_UART0</td>
</tr>
<tr>
<td style="text-align:left">0x10000100</td>
<td style="text-align:left">0x10001000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x10001000</td>
<td style="text-align:left">0x10002000</td>
<td style="text-align:left">QEMU VIRT_VIRTIO</td>
</tr>
<tr>
<td style="text-align:left">0x10002000</td>
<td style="text-align:left">0x20000000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x20000000</td>
<td style="text-align:left">0x24000000</td>
<td style="text-align:left">QEMU VIRT_FLASH</td>
</tr>
<tr>
<td style="text-align:left">0x24000000</td>
<td style="text-align:left">0x30000000</td>
<td style="text-align:left">unmapped</td>
</tr>
<tr>
<td style="text-align:left">0x30000000</td>
<td style="text-align:left">0x40000000</td>
<td style="text-align:left">QEMU VIRT_PCIE_ECAM</td>
</tr>
<tr>
<td style="text-align:left">0x40000000</td>
<td style="text-align:left">0x80000000</td>
<td style="text-align:left">QEMU VIRT_PCIE_MMIO</td>
</tr>
<tr>
<td style="text-align:left">0x80000000</td>
<td style="text-align:left">0x88000000</td>
<td style="text-align:left">DRAM &#x7F3A;&#x7701; 128MB&#xFF0C;&#x5927;&#x5C0F;&#x53EF;&#x914D;&#x7F6E;</td>
</tr>
</tbody>
</table>
</blockquote>
<p>&#x4E0D;&#x8FC7;&#x4E3A;&#x4E86;&#x7B80;&#x5355;&#x8D77;&#x89C1;&#xFF0C;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x6253;&#x7B97;&#x81EA;&#x5DF1;&#x53BB;&#x89E3;&#x6790;&#x8FD9;&#x4E2A;&#x7ED3;&#x679C;&#x3002;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;Qemu &#x89C4;&#x5B9A;&#x7684; DRAM &#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x8D77;&#x59CB;&#x7269;&#x7406;&#x5730;&#x5740;&#x4E3A; <code>0x80000000</code> &#x3002;&#x800C;&#x5728; Qemu &#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>-m</code> &#x6307;&#x5B9A; RAM &#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn><mn>2</mn><mn>8</mn><mtext><mi mathvariant="normal">M</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">B</mi></mtext></mrow><annotation encoding="application/x-tex">128\text{MiB}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">1</span><span class="mord mathrm">2</span><span class="mord mathrm">8</span><span class="mord text textstyle uncramped"><span class="mord mathrm">MiB</span></span></span></span></span> &#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684; DRAM &#x7269;&#x7406;&#x5185;&#x5B58;&#x5730;&#x5740;&#x8303;&#x56F4;&#x5C31;&#x662F; <code>[0x80000000,0x88000000)</code> &#x3002;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x5C06; DRAM &#x7269;&#x7406;&#x5185;&#x5B58;&#x7ED3;&#x675F;&#x5730;&#x5740;&#x786C;&#x7F16;&#x7801;&#x5230;&#x5185;&#x6838;&#x4E2D;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/lib.rs</span>

<span class="token keyword">mod</span> <span class="token module-declaration namespace">consts</span><span class="token punctuation">;</span>

<span class="token comment">// src/consts.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">PHYSICAL_MEMORY_END</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x88000000</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4F46;&#x662F;&#xFF0C;&#x6709;&#x4E00;&#x90E8;&#x5206; DRAM &#x7A7A;&#x95F4;&#x5DF2;&#x7ECF;&#x88AB;&#x5360;&#x7528;&#xFF0C;&#x4E0D;&#x80FD;&#x7528;&#x6765;&#x5B58;&#x522B;&#x7684;&#x4E1C;&#x897F;&#x4E86;&#xFF01;</p>
<ul>
<li>&#x7269;&#x7406;&#x5730;&#x5740;&#x7A7A;&#x95F4; <code>[0x80000000,0x80200000)</code> &#x88AB; OpenSBI &#x5360;&#x7528;&#xFF1B;</li>
<li>&#x7269;&#x7406;&#x5730;&#x5740;&#x7A7A;&#x95F4; <code>[0x80200000,KernelEnd)</code> &#x88AB;&#x5185;&#x6838;&#x5404;&#x4EE3;&#x7801;&#x4E0E;&#x6570;&#x636E;&#x6BB5;&#x5360;&#x7528;&#xFF1B;</li>
<li>&#x5176;&#x5B9E;&#x8BBE;&#x5907;&#x6811;&#x626B;&#x63CF;&#x7ED3;&#x679C; DTB &#x8FD8;&#x5360;&#x7528;&#x4E86;&#x4E00;&#x90E8;&#x5206;&#x7269;&#x7406;&#x5185;&#x5B58;&#xFF0C;&#x4E0D;&#x8FC7;&#x7531;&#x4E8E;&#x6211;&#x4EEC;&#x4E0D;&#x6253;&#x7B97;&#x4F7F;&#x7528;&#x5B83;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x5C06;&#x5B83;&#x6240;&#x5360;&#x7528;&#x7684;&#x7A7A;&#x95F4;&#x7528;&#x6765;&#x5B58;&#x522B;&#x7684;&#x4E1C;&#x897F;&#x3002;</li>
</ul>
<p>&#x4E8E;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5B58;&#x522B;&#x7684;&#x4E1C;&#x897F;&#x7684;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x7269;&#x7406;&#x5730;&#x5740;&#x8303;&#x56F4;&#x662F;&#xFF1A;<code>[KernelEnd, 0x88000000)</code> &#x3002;&#x8FD9;&#x91CC;&#x7684; <code>KernelEnd&#x200B;</code> &#x4E3A;&#x5185;&#x6838;&#x4EE3;&#x7801;&#x7ED3;&#x5C3E;&#x7684;&#x7269;&#x7406;&#x5730;&#x5740;&#x3002;&#x5728; <code>linker64.ld</code> &#x4E2D;&#x5B9A;&#x4E49;&#x7684; <code>end</code> &#x7B26;&#x53F7;&#x4E3A;&#x5185;&#x6838;&#x4EE3;&#x7801;&#x7ED3;&#x5C3E;&#x7684;&#x865A;&#x62DF;&#x5730;&#x5740;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x504F;&#x79FB;&#x91CF;&#x6765;&#x5C06;&#x5176;&#x8F6C;&#x5316;&#x4E3A;&#x7269;&#x7406;&#x5730;&#x5740;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x5C06;&#x53EF;&#x7528;&#x7684;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5730;&#x5740;&#x8303;&#x56F4;&#x6253;&#x5370;&#x51FA;&#x6765;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/consts.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x80200000</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">KERNEL_BEGIN_VADDR</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x80200000</span><span class="token punctuation">;</span>

<span class="token comment">// src/init.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>consts<span class="token punctuation">::</span></span><span class="token operator">*</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token keyword">fn</span> <span class="token function-definition function">rust_main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token punctuation">{</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span>
        <span class="token string">&quot;free physical memory paddr = [{:#x}, {:#x})&quot;</span><span class="token punctuation">,</span>
        end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> <span class="token constant">KERNEL_BEGIN_VADDR</span> <span class="token operator">+</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">,</span>
        <span class="token constant">PHYSICAL_MEMORY_END</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>interrupt<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>timer<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><strong>[success] &#x53EF;&#x7528;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5730;&#x5740; </strong></p>
<p><code>free physical memory paddr = [0x8020b000, 0x88000000)</code></p>
</blockquote>
<h3 id="&#x7269;&#x7406;&#x9875;&#x5E27;&#x4E0E;&#x7269;&#x7406;&#x9875;&#x53F7;">&#x7269;&#x7406;&#x9875;&#x5E27;&#x4E0E;&#x7269;&#x7406;&#x9875;&#x53F7;</h3>
<p>&#x901A;&#x5E38;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x5206;&#x914D;&#x7269;&#x7406;&#x5185;&#x5B58;&#x65F6;&#x5E76;&#x4E0D;&#x662F;&#x4EE5;&#x5B57;&#x8282;&#x4E3A;&#x5355;&#x4F4D;&#xFF0C;&#x800C;&#x662F;&#x4EE5;&#x4E00;<strong>&#x7269;&#x7406;&#x9875;&#x5E27;(Frame)</strong>&#xFF0C;&#x5373;&#x8FDE;&#x7EED;&#x7684; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mrow><mn>1</mn><mn>2</mn></mrow></msup><mo>=</mo><mn>4</mn><mn>0</mn><mn>9</mn><mn>6</mn></mrow><annotation encoding="application/x-tex">2^{12}=4096</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="msupsub"><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">4</span><span class="mord mathrm">0</span><span class="mord mathrm">9</span><span class="mord mathrm">6</span></span></span></span> &#x5B57;&#x8282;&#x4E3A;&#x5355;&#x4F4D;&#x5206;&#x914D;&#x3002;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x7528;<strong>&#x7269;&#x7406;&#x9875;&#x53F7;(Physical Page Number, PPN)</strong> &#x6765;&#x4EE3;&#x8868;&#x4E00;&#x7269;&#x7406;&#x9875;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x4EE3;&#x8868;&#x7269;&#x7406;&#x5730;&#x5740;&#x8303;&#x56F4;&#x5728; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>[</mo><mtext><mi mathvariant="normal">P</mi><mi mathvariant="normal">P</mi><mi mathvariant="normal">N</mi></mtext><mo>&#xD7;</mo><msup><mn>2</mn><mrow><mn>1</mn><mn>2</mn></mrow></msup><mo separator="true">,</mo><mo>(</mo><mtext><mi mathvariant="normal">P</mi><mi mathvariant="normal">P</mi><mi mathvariant="normal">N</mi></mtext><mo>+</mo><mn>1</mn><mo>)</mo><mo>&#xD7;</mo><msup><mn>2</mn><mrow><mn>1</mn><mn>2</mn></mrow></msup><mo>)</mo></mrow><annotation encoding="application/x-tex">[\text{PPN}\times 2^{12},(\text{PPN}+1)\times 2^{12})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:1.064108em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mopen">[</span><span class="mord text textstyle uncramped"><span class="mord mathrm">PPN</span></span><span class="mbin">&#xD7;</span><span class="mord"><span class="mord mathrm">2</span><span class="msupsub"><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mpunct">,</span><span class="mopen">(</span><span class="mord text textstyle uncramped"><span class="mord mathrm">PPN</span></span><span class="mbin">+</span><span class="mord mathrm">1</span><span class="mclose">)</span><span class="mbin">&#xD7;</span><span class="mord"><span class="mord mathrm">2</span><span class="msupsub"><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mclose">)</span></span></span></span> &#x7684;&#x4E00;&#x7269;&#x7406;&#x9875;&#x3002;</p>
<p>&#x4E0D;&#x96BE;&#x770B;&#x51FA;&#xFF0C;&#x7269;&#x7406;&#x9875;&#x53F7;&#x4E0E;&#x7269;&#x7406;&#x9875;&#x5F62;&#x6210;&#x4E00;&#x4E00;&#x6620;&#x5C04;&#x3002;&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x7269;&#x7406;&#x9875;&#x53F7;&#x8FD9;&#x79CD;&#x8868;&#x8FBE;&#x65B9;&#x5F0F;&#xFF0C;&#x6BCF;&#x4E2A;&#x7269;&#x7406;&#x9875;&#x7684;&#x5F00;&#x5934;&#x5730;&#x5740;&#x5FC5;&#x987B;&#x662F; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mrow><mn>1</mn><mn>2</mn></mrow></msup><mo>=</mo><mn>4</mn><mn>0</mn><mn>9</mn><mn>6</mn></mrow><annotation encoding="application/x-tex">2^{12}=4096</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="msupsub"><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">4</span><span class="mord mathrm">0</span><span class="mord mathrm">9</span><span class="mord mathrm">6</span></span></span></span> &#x7684;&#x500D;&#x6570;&#x3002;&#x4F46;&#x8FD9;&#x4E5F;&#x7ED9;&#x4E86;&#x6211;&#x4EEC;&#x4E00;&#x4E2A;&#x65B9;&#x4FBF;&#xFF1A;&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;&#x7269;&#x7406;&#x5730;&#x5740;&#xFF0C;&#x5176;&#x9664;&#x4EE5; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>4</mn><mn>0</mn><mn>9</mn><mn>6</mn></mrow><annotation encoding="application/x-tex">4096</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">4</span><span class="mord mathrm">0</span><span class="mord mathrm">9</span><span class="mord mathrm">6</span></span></span></span> (&#x6216;&#x8005;&#x8BF4;&#x53F3;&#x79FB; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn><mn>2</mn></mrow><annotation encoding="application/x-tex">12</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">1</span><span class="mord mathrm">2</span></span></span></span> &#x4F4D;) &#x7684;&#x5546;&#x5373;&#x4E3A;&#x8FD9;&#x4E2A;&#x7269;&#x7406;&#x5730;&#x5740;&#x6240;&#x5728;&#x7684;&#x7269;&#x7406;&#x9875;&#x53F7;&#x3002;</p>
<p>&#x4EE5;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;&#x53EF;&#x7528;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x7269;&#x7406;&#x9875;&#x53F7;&#x8868;&#x8FBE;&#x3002;&#x5C06; <code>init.rs</code> &#x4E2D;&#x7684;&#x8F93;&#x51FA;&#x8BED;&#x53E5;&#x7565;&#x505A;&#x6539;&#x52A8;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/init.rs</span>

<span class="token macro property">println!</span><span class="token punctuation">(</span>
        <span class="token string">&quot;free physical memory ppn = [{:#x}, {:#x})&quot;</span><span class="token punctuation">,</span>
        <span class="token punctuation">(</span><span class="token punctuation">(</span>end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> <span class="token constant">KERNEL_BEGIN_VADDR</span> <span class="token operator">+</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">)</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token constant">PHYSICAL_MEMORY_END</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<blockquote>
<p><strong>[success] &#x53EF;&#x7528;&#x7269;&#x7406;&#x9875;&#x53F7;&#x533A;&#x95F4;</strong></p>
<p><code>free physical memory ppn = [0x8020c, 0x88000)</code></p>
</blockquote>
<h3 id="&#x7269;&#x7406;&#x5185;&#x5B58;&#x9875;&#x5F0F;&#x7BA1;&#x7406;">&#x7269;&#x7406;&#x5185;&#x5B58;&#x9875;&#x5F0F;&#x7BA1;&#x7406;</h3>
<p>&#x5BF9;&#x4E8E;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x9875;&#x5F0F;&#x7BA1;&#x7406;&#x800C;&#x8A00;&#xFF0C;&#x6211;&#x4EEC;&#x6240;&#x8981;&#x652F;&#x6301;&#x7684;&#x64CD;&#x4F5C;&#x662F;&#xFF1A;</p>
<ol>
<li>&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x7269;&#x7406;&#x9875;&#xFF0C;&#x8FD4;&#x56DE;&#x5176;&#x7269;&#x7406;&#x9875;&#x53F7;&#xFF1B;</li>
<li>&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x7269;&#x7406;&#x9875;&#x53F7;&#xFF0C;&#x56DE;&#x6536;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x7269;&#x7406;&#x9875;&#x3002;</li>
<li>&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x9875;&#x53F7;&#x533A;&#x95F4;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x3002;</li>
</ol>
<p>&#x6211;&#x4EEC;&#x8003;&#x8651;&#x7528;&#x4E00;&#x9897;&#x975E;&#x9012;&#x5F52;&#x7EBF;&#x6BB5;&#x6811;&#x6765;&#x7EF4;&#x62A4;&#x8FD9;&#x4E9B;&#x64CD;&#x4F5C;&#x3002;&#x8282;&#x70B9;&#x4E0A;&#x7684;&#x503C;&#x5B58;&#x7684;&#x662F; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>0</mn><mi mathvariant="normal">/</mi><mn>1</mn></mrow><annotation encoding="application/x-tex">0/1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">0</span><span class="mord mathrm">/</span><span class="mord mathrm">1</span></span></span></span> &#x8868;&#x793A;&#x8FD9;&#x4E2A;&#x8282;&#x70B9;&#x5BF9;&#x5E94;&#x7684;&#x533A;&#x95F4;&#x5185;&#x662F;&#x5426;&#x8FD8;&#x6709;&#x7A7A;&#x95F2;&#x7269;&#x7406;&#x9875;&#xFF08;0=&#x7A7A;&#x95F2;&#xFF0C;1=&#x88AB;&#x5360;&#x7528;&#xFF09;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/const.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">MAX_PHYSICAL_MEMORY</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x8000000</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">MAX_PHYSICAL_PAGES</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token constant">MAX_PHYSICAL_MEMORY</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span><span class="token punctuation">;</span>

<span class="token comment">// src/lib.rs</span>

<span class="token keyword">mod</span> <span class="token module-declaration namespace">memory</span><span class="token punctuation">;</span>

<span class="token comment">// src/memory/mod.rs</span>

<span class="token keyword">mod</span> <span class="token module-declaration namespace">frame_allocator</span><span class="token punctuation">;</span>

<span class="token comment">// src/memory/frame_allocator.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>consts<span class="token punctuation">::</span></span><span class="token constant">MAX_PHYSICAL_PAGES</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">SegmentTreeAllocator</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">u8</span><span class="token punctuation">;</span> <span class="token constant">MAX_PHYSICAL_PAGES</span> <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    m<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
    n<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
    offset<span class="token punctuation">:</span> <span class="token keyword">usize</span>
<span class="token punctuation">}</span>

<span class="token keyword">impl</span> <span class="token class-name">SegmentTreeAllocator</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x4F7F;&#x7528;&#x7269;&#x7406;&#x9875;&#x53F7;&#x533A;&#x95F4; [l,r) &#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> l<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> r<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>offset <span class="token operator">=</span> l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>n <span class="token operator">=</span> r <span class="token operator">-</span> l<span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">&lt;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>n <span class="token operator">+</span> <span class="token number">2</span> <span class="token punctuation">{</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token keyword">self</span><span class="token punctuation">.</span>n<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">+</span> i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token keyword">self</span><span class="token punctuation">.</span>m<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">rev</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>i <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token punctuation">(</span>i <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x5206;&#x914D;&#x4E00;&#x4E2A;&#x7269;&#x7406;&#x9875;</span>
    <span class="token comment">// &#x81EA;&#x4E0A;&#x800C;&#x4E0B;&#x5BFB;&#x627E;&#x53EF;&#x7528;&#x7684;&#x6700;&#x5C0F;&#x7269;&#x7406;&#x9875;&#x53F7;</span>
    <span class="token comment">// &#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x5B9A;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x7269;&#x7406;&#x9875;&#x8017;&#x5C3D;&#x7684;&#x60C5;&#x51B5;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">alloc</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
        <span class="token comment">// assume that we never run out of physical memory</span>
        <span class="token keyword">if</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">{</span>
            <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;physical memory depleted!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> p <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> p <span class="token operator">&lt;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span> p <span class="token operator">=</span> p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> p <span class="token operator">=</span> <span class="token punctuation">(</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">let</span> result <span class="token operator">=</span> p <span class="token operator">+</span> <span class="token keyword">self</span><span class="token punctuation">.</span>offset <span class="token operator">-</span> <span class="token keyword">self</span><span class="token punctuation">.</span>m<span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        p <span class="token operator">&gt;&gt;=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> p <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token punctuation">(</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            p <span class="token operator">&gt;&gt;=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        result
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x56DE;&#x6536;&#x7269;&#x7406;&#x9875;&#x53F7;&#x4E3A; n &#x7684;&#x7269;&#x7406;&#x9875;</span>
    <span class="token comment">// &#x81EA;&#x4E0B;&#x800C;&#x4E0A;&#x8FDB;&#x884C;&#x66F4;&#x65B0;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">dealloc</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> n<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> p <span class="token operator">=</span> n <span class="token operator">+</span> <span class="token keyword">self</span><span class="token punctuation">.</span>m <span class="token operator">-</span> <span class="token keyword">self</span><span class="token punctuation">.</span>offset<span class="token punctuation">;</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        p <span class="token operator">&gt;&gt;=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> p <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token punctuation">(</span>p <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            p <span class="token operator">&gt;&gt;=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E8B;&#x5B9E;&#x4E0A;&#x6BCF;&#x6B21;&#x5206;&#x914D;&#x7684;&#x662F;&#x53EF;&#x7528;&#x7684;&#x7269;&#x7406;&#x9875;&#x53F7;&#x6700;&#x5C0F;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x65B9;&#x9762;&#x5C31;&#x4E0D;&#x8D58;&#x8FF0;&#x4E86;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x5C06;&#x8FD9;&#x4E2A;&#x7C7B;&#x5B9E;&#x4F8B;&#x5316;&#x5E76;&#x58F0;&#x660E;&#x4E3A; <code>static</code> &#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5728;&#x6574;&#x4E2A;&#x7A0B;&#x5E8F; &#x8FD0;&#x884C;&#x8FC7;&#x7A0B;&#x5F53;&#x4E2D;&#x5747;&#x6709;&#x6548;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/Cargo.toml</span>

<span class="token punctuation">[</span>dependencies<span class="token punctuation">]</span>
spin <span class="token operator">=</span> <span class="token string">&quot;0.5.2&quot;</span>

<span class="token comment">// src/memory/frame_allocator.rs</span>

<span class="token keyword">use</span> <span class="token namespace">spin<span class="token punctuation">::</span></span><span class="token class-name">Mutex</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">static</span> <span class="token constant">SEGMENT_TREE_ALLOCATOR</span><span class="token punctuation">:</span> <span class="token class-name">Mutex</span><span class="token operator">&lt;</span><span class="token class-name">SegmentTreeAllocator</span><span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token class-name">Mutex</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">SegmentTreeAllocator</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token constant">MAX_PHYSICAL_PAGES</span> <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    m<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    n<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    offset<span class="token punctuation">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6CE8;&#x610F;&#x5230;&#x5728;&#x5185;&#x6838;&#x4E2D;&#x5F00;&#x4E86;&#x4E00;&#x5757;&#x6BD4;&#x8F83;&#x5927;&#x7684;&#x9759;&#x6001;&#x5185;&#x5B58;&#xFF0C;<code>a</code> &#x6570;&#x7EC4;&#x3002;&#x90A3;&#x4E48; <code>a</code> &#x6570;&#x7EC4;&#x7A76;&#x7ADF;&#x6709;&#x591A;&#x5927;&#x5462;&#xFF1F;&#x5B9E;&#x9645;&#x4E0A; <code>a</code> &#x6570;&#x7EC4;&#x7684;&#x5927;&#x5C0F;&#x4E3A;&#x6700;&#x5927;&#x53EF;&#x80FD;&#x7269;&#x7406;&#x9875;&#x6570;&#x7684;&#x4E8C;&#x500D;&#xFF0C;&#x56E0;&#x6B64; <code>a</code> &#x6570;&#x7EC4;&#x5927;&#x5C0F;&#x4EC5;&#x4E3A;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5927;&#x5C0F;&#x7684; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mfrac><mrow><mn>1</mn></mrow><mrow><msup><mn>2</mn><mrow><mn>1</mn><mn>2</mn></mrow></msup></mrow></mfrac><mo>&#xD7;</mo><mn>2</mn><mo>&#x2243;</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>5</mn><mi mathvariant="normal">%</mi></mrow><annotation encoding="application/x-tex">\frac{1}{2^{12}}\times 2\simeq 0.05\%</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.190108em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mtight"><span class="mord mathrm mtight">2</span><span class="msupsub"><span class="vlist"><span style="top:-0.286em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span><span class="mbin">&#xD7;</span><span class="mord mathrm">2</span><span class="mrel">&#x2243;</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">5</span><span class="mord mathrm">%</span></span></span></span>&#xFF0C;&#x53EF;&#x8BF4;&#x662F;&#x5FAE;&#x4E4E;&#x5176;&#x5FAE;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x672C;&#x6765;&#x60F3;&#x628A; <code>SEGMENT_TREE_ALLOCATOR</code> &#x58F0;&#x660E;&#x4E3A; <code>static mut</code> &#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x9996;&#x5148;&#x5B83;&#x9700;&#x8981;&#x662F; <code>static</code> &#x7C7B;&#x578B;&#x7684;&#xFF1B;&#x5176;&#x6B21;&#xFF0C;&#x5B83;&#x7684;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5; <code>init, alloc, dealloc</code> &#x90FD;&#x9700;&#x8981;&#x4FEE;&#x6539;&#x81EA;&#x8EAB;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x5BF9;&#x4E8E; <code>static mut</code> &#x7C7B;&#x578B;&#x7684;&#x4FEE;&#x6539;&#x64CD;&#x4F5C;&#x662F; <code>unsafe</code> &#x7684;&#x3002;&#x6211;&#x4EEC;&#x4E4B;&#x540E;&#x4F1A;&#x63D0;&#x5230;<strong>&#x7EBF;&#x7A0B;</strong>&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x5BF9;&#x4E8E; <code>static</code> &#x7C7B;&#x578B;&#x7684;&#x9759;&#x6001;&#x6570;&#x636E;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x90FD;&#x80FD;&#x8BBF;&#x95EE;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6B63;&#x5728;&#x8BBF;&#x95EE;&#x8FD9;&#x6BB5;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E5F;&#x6765;&#x8BBF;&#x95EE;&#xFF0C;&#x5C31;&#x53EF;&#x80FD;&#x4F1A;&#x4EA7;&#x751F;&#x51B2;&#x7A81;&#xFF0C;&#x5E76;&#x5E26;&#x6765;&#x96BE;&#x4EE5;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528; <code>spin::Mutex<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>T</span><span class="token punctuation">&gt;</span></span></code> &#x7ED9;&#x8FD9;&#x6BB5;&#x6570;&#x636E;&#x52A0;&#x4E00;&#x628A;&#x9501;&#xFF0C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BD5;&#x56FE;&#x901A;&#x8FC7; <code>.lock()</code> &#x6253;&#x5F00;&#x9501;&#x6765;&#x83B7;&#x53D6;&#x5185;&#x90E8;&#x6570;&#x636E;&#x7684;&#x53EF;&#x53D8;&#x5F15;&#x7528;&#xFF0C;&#x5982;&#x679C;&#x94A5;&#x5319;&#x88AB;&#x522B;&#x7684;&#x7EBF;&#x7A0B;&#x6240;&#x5360;&#x7528;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x5C31;&#x4F1A;&#x4E00;&#x76F4;&#x5361;&#x5728;&#x8FD9;&#x91CC;&#xFF1B;&#x76F4;&#x5230;&#x90A3;&#x4E2A;&#x5360;&#x7528;&#x4E86;&#x94A5;&#x5319;&#x7684;&#x7EBF;&#x7A0B;&#x5BF9;&#x5185;&#x90E8;&#x6570;&#x636E;&#x7684;&#x8BBF;&#x95EE;&#x7ED3;&#x675F;&#xFF0C;&#x9501;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x5C06;&#x94A5;&#x5319;&#x4EA4;&#x8FD8;&#x51FA;&#x6765;&#xFF0C;&#x88AB;&#x5361;&#x4F4F;&#x7684;&#x90A3;&#x4E2A;&#x7EBF;&#x7A0B;&#x62FF;&#x5230;&#x4E86;&#x94A5;&#x5319;&#xFF0C;&#x5C31;&#x53EF;&#x6253;&#x5F00;&#x9501;&#x83B7;&#x53D6;&#x5185;&#x90E8;&#x5F15;&#x7528;&#xFF0C;&#x8BBF;&#x95EE;&#x5185;&#x90E8;&#x6570;&#x636E;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x7684;&#x662F; <code>spin::Mutex<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>T</span><span class="token punctuation">&gt;</span></span></code> &#xFF0C; &#x6211;&#x4EEC;&#x5728; <code>Cargo.toml</code> &#x4E2D;&#x6DFB;&#x52A0;&#x4F9D;&#x8D56;&#x3002;&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;&#x5B83;&#x4E5F;&#x65E0;&#x9700;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x652F;&#x6301;&#xFF08;&#x5373;&#x652F;&#x6301; no_std&#xFF09;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x653E;&#x5FC3;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5728; <code>src/memory/mod.rs</code> &#x91CC;&#x9762;&#x518D;&#x5BF9;&#x8FD9;&#x4E2A;&#x7C7B;&#x5305;&#x88C5;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/memory/mod.rs</span>

<span class="token keyword">use</span> <span class="token namespace">frame_allocator<span class="token punctuation">::</span></span><span class="token constant">SEGMENT_TREE_ALLOCATOR</span> <span class="token keyword">as</span> <span class="token constant">FRAME_ALLOCATOR</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">riscv<span class="token punctuation">::</span>addr<span class="token punctuation">::</span></span><span class="token punctuation">{</span>
    <span class="token comment">// &#x5206;&#x522B;&#x4E3A;&#x865A;&#x62DF;&#x5730;&#x5740;&#x3001;&#x7269;&#x7406;&#x5730;&#x5740;&#x3001;&#x865A;&#x62DF;&#x9875;&#x3001;&#x7269;&#x7406;&#x9875;&#x5E27;</span>
    <span class="token comment">// &#x975E;&#x5E38;&#x65B9;&#x4FBF;&#xFF0C;&#x4E4B;&#x540E;&#x4F1A;&#x7ECF;&#x5E38;&#x7528;&#x5230;</span>
    <span class="token comment">// &#x7528;&#x6CD5;&#x53EF;&#x53C2;&#x89C1; https://github.com/rcore-os/riscv/blob/master/src/addr.rs</span>
    <span class="token class-name">VirtAddr</span><span class="token punctuation">,</span>
    <span class="token class-name">PhysAddr</span><span class="token punctuation">,</span>
    <span class="token class-name">Page</span><span class="token punctuation">,</span>
    <span class="token class-name">Frame</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span>l<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> r<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">FRAME_ALLOCATOR</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;++++ setup memory!    ++++&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">Frame</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x5C06;&#x7269;&#x7406;&#x9875;&#x53F7;&#x8F6C;&#x4E3A;&#x7269;&#x7406;&#x9875;&#x5E27;</span>
    <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">::</span><span class="token function">of_ppn</span><span class="token punctuation">(</span><span class="token constant">FRAME_ALLOCATOR</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">alloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">dealloc_frame</span><span class="token punctuation">(</span>f<span class="token punctuation">:</span> <span class="token class-name">Frame</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">FRAME_ALLOCATOR</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">dealloc</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">number</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x5B83;&#x662F;&#x5426;&#x80FD;&#x591F;&#x5F88;&#x597D;&#x7684;&#x5B8C;&#x6210;&#x7269;&#x7406;&#x9875;&#x5206;&#x914D;&#x4E0E;&#x56DE;&#x6536;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/init.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>memory<span class="token punctuation">::</span></span><span class="token punctuation">{</span>
    alloc_frame<span class="token punctuation">,</span>
    dealloc_frame
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token keyword">fn</span> <span class="token function-definition function">rust_main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token punctuation">{</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;kernel end vaddr = {:#x}&quot;</span><span class="token punctuation">,</span> end <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span>
        <span class="token string">&quot;free physical memory ppn = [{:#x}, {:#x})&quot;</span><span class="token punctuation">,</span>
        <span class="token punctuation">(</span><span class="token punctuation">(</span>end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> <span class="token constant">KERNEL_BEGIN_VADDR</span> <span class="token operator">+</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">)</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token constant">PHYSICAL_MEMORY_END</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>interrupt<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>memory<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span>
        <span class="token punctuation">(</span><span class="token punctuation">(</span>end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> <span class="token constant">KERNEL_BEGIN_VADDR</span> <span class="token operator">+</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">)</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token constant">PHYSICAL_MEMORY_END</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">frame_allocating_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>timer<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token macro property">asm!</span><span class="token punctuation">(</span><span class="token string">&quot;ebreak&quot;</span><span class="token punctuation">::</span><span class="token punctuation">::</span><span class="token string">&quot;volatile&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;end of rust_main&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">fn</span> <span class="token function-definition function">frame_allocating_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;alloc {:x?}&quot;</span><span class="token punctuation">,</span> <span class="token function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;alloc {:x?}&quot;</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;alloc {:x?}&quot;</span><span class="token punctuation">,</span> <span class="token function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;dealloc {:x?}&quot;</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dealloc_frame</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;alloc {:x?}&quot;</span><span class="token punctuation">,</span> <span class="token function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;alloc {:x?}&quot;</span><span class="token punctuation">,</span> <span class="token function">alloc_frame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x5C1D;&#x8BD5;&#x5728;&#x5206;&#x914D;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x56DE;&#x6536;&#xFF0C;&#x4E4B;&#x540E;&#x518D;&#x8FDB;&#x884C;&#x5206;&#x914D;&#xFF0C;&#x7ED3;&#x679C;&#x5982;&#x4F55;&#x5462;&#xFF1F;</p>
<blockquote>
<p><strong>[success] &#x7269;&#x7406;&#x9875;&#x5206;&#x914D;&#x4E0E;&#x56DE;&#x6536;&#x6D4B;&#x8BD5;</strong></p>
<pre class="language-"><code class="lang-rust">free physical memory paddr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0x8021f020</span><span class="token punctuation">,</span> <span class="token number">0x88000000</span><span class="token punctuation">)</span>
free physical memory ppn <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0x80220</span><span class="token punctuation">,</span> <span class="token number">0x88000</span><span class="token punctuation">)</span>
<span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span> setup <span class="token macro property">interrupt!</span> <span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span>
<span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span> setup <span class="token macro property">timer!</span>     <span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span>
<span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span> setup <span class="token macro property">memory!</span>    <span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span><span class="token operator">+</span>
alloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80220000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
alloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80221000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
alloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80222000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
dealloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80221000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
alloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80221000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
alloc <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Frame</span><span class="token punctuation">(</span><span class="token class-name">PhysAddr</span><span class="token punctuation">(</span><span class="token number">80223000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token operator">*</span> <span class="token number">100</span> ticks <span class="token operator">*</span>
<span class="token operator">*</span> <span class="token number">100</span> ticks <span class="token operator">*</span>
<span class="token punctuation">...</span>
</code></pre>
</blockquote>
<p>&#x6211;&#x4EEC;&#x56DE;&#x6536;&#x7684;&#x9875;&#x9762;&#x63A5;&#x4E0B;&#x6765;&#x9A6C;&#x4E0A;&#x5C31;&#x53C8;&#x88AB;&#x5206;&#x914D;&#x51FA;&#x53BB;&#x4E86;&#x3002;</p>
<p>&#x5982;&#x679C;&#x7ED3;&#x679C;&#x6709;&#x95EE;&#x9898;&#x7684;&#x8BDD;&#xFF0C;&#x5728;<a href="https://github.com/rcore-os/rCore_tutorial/tree/ch4-pa1" target="_blank">&#x8FD9;&#x91CC;</a>&#x80FD;&#x627E;&#x5230;&#x73B0;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x4E0D;&#x8FC7;&#xFF0C;&#x8FD9;&#x79CD;&#x7269;&#x7406;&#x5185;&#x5B58;&#x5206;&#x914D;&#x7ED9;&#x4EBA;&#x4E00;&#x79CD;&#x8FC7;&#x5BB6;&#x5BB6;&#x7684;&#x611F;&#x89C9;&#x3002;&#x65E0;&#x8BBA;&#x8868;&#x9762;&#x4E0A;&#x5206;&#x914D;&#x3001;&#x56DE;&#x6536;&#x505A;&#x5F97;&#x600E;&#x6837;&#x4E95;&#x4E95;&#x6709;&#x6761;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x90FD;&#x5E76;&#x6CA1;&#x6709;&#x5BF9;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4EA7;&#x751F;&#x4EFB;&#x4F55;&#x5F71;&#x54CD;&#xFF01;&#x4E0D;&#x8981;&#x7740;&#x6025;&#xFF0C;&#x6211;&#x4EEC;&#x4E4B;&#x540E;&#x4F1A;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x7684;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="introduction.html" class="navigation navigation-prev " aria-label="Previous page: 第四章：内存管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part2.html" class="navigation navigation-next " aria-label="Next page: [动态内存分配]">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"[物理内存探测与管理]","level":"1.6.1","depth":2,"next":{"title":"[动态内存分配]","level":"1.6.2","depth":2,"path":"chapter4/part2.md","ref":"chapter4/part2.md","articles":[]},"previous":{"title":"第四章：内存管理","level":"1.6","depth":1,"path":"chapter4/introduction.md","ref":"chapter4/introduction.md","articles":[{"title":"[物理内存探测与管理]","level":"1.6.1","depth":2,"path":"chapter4/part1.md","ref":"chapter4/part1.md","articles":[]},{"title":"[动态内存分配]","level":"1.6.2","depth":2,"path":"chapter4/part2.md","ref":"chapter4/part2.md","articles":[]},{"title":"总结与展望","level":"1.6.3","depth":2,"path":"chapter4/part3.md","ref":"chapter4/part3.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter4/part1.md","mtime":"2020-09-09T11:31:56.895Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

